<?php
class Slog_Controller_Response
{
	const HEADER_TITLE = 'title';
	const HEADER_META = 'meta';
	const HEADER_CSS = 'css';
	const HEADER_SCRIPT = 'script';
	
	protected $_html_headers;
	protected $_http_headers;
	
	public function __construct()
	{
		$this->_html_headers = array();
		$this->_http_headers = array();
	}
	
	public function init()
	{
		$this->addHTTPHeader('content-type', 'text/html; charset=utf-8');
	}
	
	public function out()
	{
		// Enviamos las cabeceras HTTP
		ob_clean();
		foreach ($this->_http_headers as $name=>$value) {
			header("$name: $value");
		}
		
		// Obtenemos la vista del último controlador/acción ejecutado
		$mode = Slog::router()->mode;
		$controller = Slog::dispatcher()->getLastController();
		$action = Slog::dispatcher()->getLastAction();
		
		// Obtenemos el contenido HTML del token
		Slog::view()->__content = Slog::view()->parse("$mode/$controller/$action");
		
		// Devolvemos el contenido HTML completo
		echo Slog::view()->parse("$mode/layout");
	}
	
	public function addHTTPHeader($name, $value)
	{
		$this->_http_headers[$name] = $value;
	}
	
	public function addHTMLHeader($type, $params, $position=null)
	{
		$position = $position!==null ? $position : 999;
		
		if (!isset($this->_html_headers[$type])) {
			$this->_html_headers[$type] = array();
			if (!isset($this->_html_headers[$type][$position])) {
				$this->_html_headers[$type][$position] = array();
			}
		}
		
		$this->_html_headers[$type][$position] = $params;
		
		return $this;
	}
	
	public function headerExists($type, $token, $value)
	{
		if (isset($this->_html_headers[$type])) foreach ($this->_html_headers[$type] as $headers) {
			foreach ($headers as $header) {
				if ($header[$token] == $value) return true;
			}
		}
		return false;
	}
	
	public function addScriptFile($filename, $position=null)
	{
		// Se comprueba que no exista ya el fichero
		if (!$this->headerExists(self::HEADER_SCRIPT, 'href', $filename)) {
			// Se crea la cabecera
			$this->addHTMLHeader(self::HEADER_SCRIPT, array(
				'href'	=> $filename,
			), $position);
		}
		return $this;
	}
	
	public function addCssFile($filename, $media='all', $position=null)
	{
		// Se comprueba que no exista ya el fichero
		if (!$this->headerExists(self::HEADER_CSS, 'href', $filename)) {
			// Se crea la cabecera
			$this->addHTMLHeader(self::HEADER_CSS, array(
				'href'	=> $filename,
				'media'	=> $media,
			), $position);
		}
		return $this;
	}
	
	public function addMeta($name, $content, $http=false)
	{
		// Se comprueba que no exista ya el fichero
		if (!$this->headerExists(self::HEADER_META, 'name', $name)) {
			// Se crea la cabecera
			$this->addHTMLHeader(self::HEADER_META, array(
				'name'		=> $name,
				'content'	=> $content,
				'http'		=> $http,
			));
		}
		return $this;
	}
	
	/**
	 * Establece el título.
	 *
	 * @param	string	$title
	 * @return	Slog_Controller_Response
	 */
	public function setTitle($title)
	{
		$this->_html_headers[self::HEADER_TITLE][0] = array('content' => $title);
		return $this;
	}
	
	/**
	 * Devuelve todas las cabeceras en formato HTML
	 *
	 */
	public function headers()
	{
		ob_start();
		foreach ($this->_html_headers as $type=>$headers) {
			foreach ($headers as $params) {
				switch ($type) {
					case self::HEADER_TITLE:
						echo "<title>{$params['content']}</title>" . "\n";
						break;
					case self::HEADER_META:
						$attr = $params['http'] ? 'http-equiv' : 'name';
						echo "<meta $attr='{$params['name']}' content='{$params['content']}' />" . "\n";
						break;
					case self::HEADER_CSS:
						echo "<link rel='stylesheet' type='text/css' href='{$params['href']}' media='{$params['media']}' />" . "\n";
						break;
					case self::HEADER_SCRIPT:
						echo "<script type='text/javascript' src='{$params['href']}'></script>" . "\n";
						break;
				}
			}
		}
		return ob_get_clean();
	}
}
